package com.sporteamws;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

/* To send a message to a device, the application server sends a HTTP POST request to the Google C2DM servers. 
 * This HTTP GET request contains the registration ID for this device and well as the authentication token 
 * (to tell Google that this server is allowed to send messages) */
 
public class MessageUtil
{
	private final static String AUTH = "authentication";

	private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";

	public static final String PARAM_REGISTRATION_ID = "registration_id";

	public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";

	public static final String PARAM_COLLAPSE_KEY = "collapse_key";

	private static final String UTF8 = "UTF-8";

	
	public static void SendMsgToUser(String facebook_id, String msg)
	{
		Log.w("sending msg to user", "id: " + facebook_id + "msg: " + msg);
		
		try 
		{
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet("http://sporteamws.appspot.com/c2dmMSG?facebook_id="
					+ facebook_id + "&msg=" + msg);
			client.execute(request);
		}
		catch (Exception ex)
		{
			Log.e("Http error",ex.toString());
		}
	}
	public static int sendMessage(String auth_token, String registrationId,String message) throws IOException
    {

		StringBuilder postDataBuilder = new StringBuilder();
		postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
		postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
		postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8));
				
		byte[] postData = postDataBuilder.toString().getBytes(UTF8);

		// Hit the dm URL.
		URL url = new URL("https://android.clients.google.com/c2dm/send");
		HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
				
		//setup connection
		HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		
		connection.setRequestMethod("POST");
		
		connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
				
		connection.setRequestProperty("Content-Length",Integer.toString(postData.length));
				
		connection.setRequestProperty("Authorization", "GoogleLogin auth="+ auth_token);
				
		OutputStream out = connection.getOutputStream();
		out.write(postData);
		out.close();

		//get response code, should be 200
		int responseCode = connection.getResponseCode();
		return responseCode;
	}

	private static class CustomizedHostnameVerifier implements HostnameVerifier
	{
		public boolean verify(String hostname, SSLSession session) 
		{
			return true;
		}
	}
}